home *** CD-ROM | disk | FTP | other *** search
/ Assassins - Ultimate CD Games Collection 2 / Assassins 2 - Ultimate Games No. 2 (1995)(Weird Science)[!][Amiga-CD32-CDTV].iso / disks / asi105.dms / asi105.adf / RUMMY / SOURCE / game.c.pp / game.c
C/C++ Source or Header  |  1991-02-24  |  10KB  |  431 lines

  1. /* GAME.C
  2.  * Main program, with main() function. Must still #include externs.h for 
  3.  * function prototypes.
  4.  */
  5.  
  6. #include    <proto/all.h>
  7. #include    <stdlib.h>
  8. #include    <intuition/intuition.h>
  9. #include    <stdio.h>
  10. #include    <math.h>
  11. #include    <time.h>
  12. #include    <libraries/dos.h>
  13. #include    "defns.h"
  14. #include    "externs.h"
  15. int    CXBRK(VOID)    { return 0;}
  16.  
  17. VOID    titles(VOID);
  18. VOID    main(VOID);
  19.  
  20. VOID    main()
  21. {
  22.     struct    IntuiMessage    *msg;
  23.     ULONG    class,mnum,inum,snum;
  24.     USHORT    code,game;
  25.     unsigned    int    clock[2];
  26.     fclose(stdin); fclose(stdout); /*get rid of window opened automatically
  27.                             on the WorkBench screen*/
  28.     timer(clock);
  29.     srand(clock[1]); /*seed random number generator*/
  30.     openall();
  31.     initialisepack();
  32.     DrawImage(wind->RPort,packim[0][0],FUPX,FUPY);
  33.     DrawImage(wind->RPort,packim[0][0],FDPX,FDPY);
  34.     AddGList(wind,&stdgad[3],-1,5,NULL);
  35.     RefreshGList(&stdgad[3],wind,NULL,5);
  36.     RemoveGList(wind,&stdgad[3],5); /*display standard gadgets*/
  37.     titles();
  38.     ScreenToFront(scr);
  39.     
  40.     while(!quit)
  41.     {
  42.         Wait(1l << mp->mp_SigBit);
  43.         game=0;
  44.         while(msg=(struct IntuiMessage *)GetMsg(mp))
  45.         {
  46.             class = msg->Class;
  47.             code = msg->Code;
  48.             gaddr = (struct Gadget *)msg->IAddress;
  49.             ReplyMsg((struct Message *)msg);
  50.             switch(class)
  51.             {
  52.             case GADGETUP:
  53.                 break;
  54.             case MENUPICK:
  55.                 while(code!=MENUNULL)
  56.                 {
  57.                     mnum=MENUNUM(code);
  58.                     inum=ITEMNUM(code);
  59.                     snum=SUBNUM(code);
  60.                     switch(mnum)
  61.                     {
  62.                     case PROJMENU:
  63.                         switch(inum)
  64.                         {
  65.                         case 0:/* play */
  66.                             game = snum+1; /*process other messages first*/
  67.                             break;
  68.                         case 2:/* about */
  69.                             about();    
  70.                         }
  71.                     }
  72.                     code=((struct MenuItem *)ItemAddress(menu,(LONG)code))->NextSelect;
  73.                 }
  74.             }
  75.         }
  76.         switch(game)
  77.         {
  78.         case 1:
  79.             REMHAND(plr[1]);
  80.             REMHAND(plr[2]);
  81.             REMHAND(plr[3]);
  82.             DrawImage(wind->RPort,&setrem,0,0); 
  83.             OnMenu(wind,0x0020); OffMenu(wind,0x0000); OffMenu(wind,0x1000);
  84.             OnMenu(wind,0xf862); OnMenu(wind,0x1800);
  85.             playgame(3);
  86.             OffMenu(wind,0x0020); OnMenu(wind,0x0000); OnMenu(wind,0x1000);
  87.             OffMenu(wind,0xf862); OffMenu(wind,0x1800);
  88.             DrawImage(wind->RPort,packim[0][0],FUPX,FUPY);
  89.             DrawImage(wind->RPort,packim[0][0],FDPX,FDPY);
  90.             TXTOUT(MESX,MESY,1,0,"Use Menus To Play....")
  91.             titles();
  92.             break;
  93.         case 3:
  94.             REMHAND(plr[1]);
  95.             REMHAND(plr[2]);
  96.             REMHAND(plr[3]);
  97.             DrawImage(wind->RPort,&setrem,0,0); 
  98.             OnMenu(wind,0x0020); OffMenu(wind,0x0000); OffMenu(wind,0x1000);
  99.             OnMenu(wind,0xf862); OnMenu(wind,0x1800); OffMenu(wind,0x1062);
  100.             OffMenu(wind,0xf841); OffMenu(wind,0x1002); OffMenu(wind,0x1042);
  101.             playgame(2);
  102.             OffMenu(wind,0x0020); OnMenu(wind,0x0000); OnMenu(wind,0x1000);
  103.             OffMenu(wind,0xf862); OffMenu(wind,0x1800); OnMenu(wind,0x1062);
  104.             OnMenu(wind,0xf841); OnMenu(wind,0x1002); OnMenu(wind,0x1042);
  105.             DrawImage(wind->RPort,packim[0][0],FUPX,FUPY);
  106.             DrawImage(wind->RPort,packim[0][0],FDPX,FDPY);
  107.             TXTOUT(MESX,MESY,1,0,"Use Menus To Play....")
  108.             titles();
  109.             break;
  110.         }
  111.     }
  112.     closeall();
  113. }
  114.  
  115. VOID    openall()
  116. {
  117.     if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",33l)))
  118.     {
  119.         closeall();
  120.         exit(1);
  121.     }
  122.     if(!(scr=OpenScreen(&scrspec)))
  123.     {
  124.         closeall();
  125.         exit(1);
  126.     }
  127.     winspec.Screen = scr;
  128.     if(!(wind=OpenWindow(&winspec)))
  129.     {
  130.         closeall();
  131.         exit(1);
  132.     }
  133.     if(!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0l)))
  134.     {
  135.         closeall();
  136.         exit(1);
  137.     }
  138.     instexcp();
  139.     SetMenuStrip(wind,menu);    /*attach debug menu*/
  140.     SetRGB4(&scr->ViewPort,0,3,9,3);
  141.     SetRGB4(&scr->ViewPort,1,13,13,13);
  142.     SetRGB4(&scr->ViewPort,2,12,4,4);
  143.     SetRGB4(&scr->ViewPort,3,0,0,0);
  144.     SetRGB4(&scr->ViewPort,4,4,5,11);
  145.     SetRGB4(&scr->ViewPort,5,12,11,1);
  146.     SetRGB4(&scr->ViewPort,6,9,9,9);
  147.     SetRGB4(&scr->ViewPort,7,15,15,15);
  148.     SetRGB4(&scr->ViewPort,16,4,5,11);
  149.     SetRGB4(&scr->ViewPort,17,1,3,10);
  150.     SetRGB4(&scr->ViewPort,18,6,9,9);
  151.     SetRGB4(&scr->ViewPort,19,0,0,0);
  152.     initdisp();
  153.     SETPTR(ptrs[0])
  154. }
  155.  
  156. VOID    initdisp()
  157. {
  158.     PrintIText(wind->RPort,inittxt,0,0);
  159.     DrawImage(wind->RPort,plrids,0,0);
  160. }
  161.  
  162. VOID    instexcp()
  163. {    /*install exception handler*/
  164.     if(!(mp=CreatePort(NULL,0)))
  165.     {
  166.         closeall();
  167.         exit(2);
  168.     }
  169.     if(!(tc=FindTask(NULL)))
  170.     {
  171.         closeall();
  172.         exit(1);
  173.     }
  174.     tc->tc_ExceptCode = (APTR) excp;
  175.     SetExcept(0xffffffff,1l << wind->UserPort->mp_SigBit);
  176. }
  177.  
  178. VOID    titles()
  179. {
  180.     USHORT    i;
  181.     static    struct    TextAttr    tp60it =
  182.     { (STRPTR) "topaz.font", TOPAZ_SIXTY,FSF_ITALIC,FPF_ROMFONT };
  183.     static    struct    IntuiText    titletxt[] =
  184.     {
  185.         { 7,0,JAM1,445,70,&tp60it,"Rummy V1.00",&titletxt[1]},
  186.         { 7,0,JAM1,427,100,&tp60it,"By Simon Preene",&titletxt[2]},
  187.         { 7,0,JAM1,477,110,&tp60it,"1990",&titletxt[3]},
  188.         { 4,0,JAM1,382,140,&tp60it,"Copyright Amiga Computing",&titletxt[4]},
  189.         { 3,0,JAM1,378,170,&tp60it,"Use Project Menu To Start,",&titletxt[5]},
  190.         { 3,0,JAM1,387,180,&tp60it,"Special And Intelligence",&titletxt[6]},
  191.         { 3,0,JAM1,414,190,&tp60it,"To Select Options",NULL}
  192.     };
  193.     PrintIText(wind->RPort,titletxt,0,0);
  194.     for(i=JACK;i<=KING;i++)
  195.     {
  196.         DrawImage(wind->RPort,packim[0][CARD(i,1)],30,plr[i-JACK+1]->yc+2);
  197.         DrawImage(wind->RPort,packim[0][CARD(i,3)],100,plr[i-JACK+1]->yc+2);
  198.     }
  199. }
  200.  
  201. VOID    closeall()
  202. {
  203.     remexcp();
  204.     if(GfxBase) CloseLibrary(GfxBase);
  205.     if(wind)
  206.     {
  207.         if(wind->MenuStrip) ClearMenuStrip(wind);
  208.         CloseWindow(wind);
  209.     }
  210.     if(scr) CloseScreen(scr);
  211.     if(IntuitionBase) CloseLibrary(IntuitionBase);
  212.  
  213. }
  214.  
  215. VOID    remexcp()
  216. {    /*remove exception handler*/
  217.     struct    IntuiMessage    *m2;
  218.     if(tc) tc->tc_ExceptCode = NULL;
  219.     if(mp)
  220.     {
  221.         while(m2=(struct IntuiMessage *)GetMsg(mp)) ReplyMsg((struct Message *)m2);
  222.         DeletePort(mp);
  223.     }
  224. }
  225.  
  226. ULONG    __asm excp(register __d0 ULONG signals,register __a1 ULONG exceptData)
  227. {
  228.     static    exgmverf=1,quitverf=1;    /*flags used to decide if verify menus
  229.                                     should be enabled or disabled*/
  230.     ULONG    class,mnum,inum,snum,gid,pos;
  231.     USHORT    code,pass;    /*pass message to main program?*/
  232.     struct    IntuiMessage    *m1;
  233.     struct    MenuItem    *item;
  234.     while(m1=(struct IntuiMessage *)GetMsg(wind->UserPort))
  235.     {
  236.         pass=0;
  237.         class=m1->Class;
  238.         code=m1->Code;
  239.         switch(class)
  240.         {
  241.         case GADGETUP:
  242.             gid=((struct Gadget *)(m1->IAddress))->GadgetID;
  243.             switch(gid)
  244.             {
  245.             case CONTG:
  246.                 cont=1;
  247.                 pass=1;
  248.                 break;
  249.             case NHANDG: /* Num hand gadget */
  250.                 numhand = ((struct StringInfo *)(stdgad[7].SpecialInfo))->LongInt;
  251.                 break;
  252.             case FUPG:
  253.                 if(stdgad[5].Flags&SELECTED)
  254.                 {
  255.                     pos=RemoveGList(wind,&stdgad[6],1);
  256.                     RefreshGList(&stdgad[6],wind,NULL,1);
  257.                     stdgad[6].Flags &= ~SELECTED;
  258.                     AddGList(wind,&stdgad[6],pos,1,NULL);
  259.                     pass=1;
  260.                 }
  261.                 break;
  262.             case FDPG:
  263.                 if(stdgad[6].Flags&SELECTED)
  264.                 {
  265.                     pos=RemoveGList(wind,&stdgad[5],1);
  266.                     RefreshGList(&stdgad[5],wind,NULL,1);
  267.                     stdgad[5].Flags &= ~SELECTED;
  268.                     AddGList(wind,&stdgad[5],pos,1,NULL);
  269.                     pass=1;
  270.                 }
  271.                 break;
  272.  
  273.             default:
  274.                 pass=1;
  275.             }
  276.             break;
  277.         case MENUPICK:
  278.             while(code!=MENUNULL)
  279.             {
  280.                 item=ItemAddress(menu,code);
  281.                 mnum=MENUNUM(code);
  282.                 inum=ITEMNUM(code);
  283.                 snum=SUBNUM(code);
  284.                 switch(mnum)
  285.                 {
  286.                 case PROJMENU:
  287.                     switch(inum)
  288.                     {
  289.                     case 0:
  290.                         if(snum==3)
  291.                         { /*exit game*/
  292.                             exgmverf = !exgmverf;
  293.                             if(!exgmverf)
  294.                             {
  295.                                 OnMenu(wind,0xf803);
  296.                                 SetWindowTitles(wind,NULL,"Rummy    <END GAME>: Use Verify Menu To Confirm");
  297.                             }
  298.                             else
  299.                             { /*reselect to cancel*/
  300.                                 OffMenu(wind,0xf803);
  301.                                 if(quitverf) SetWindowTitles(wind,NULL,"Rummy by Simon Preene, Copyright Amiga Computing 1990");
  302.                                 else SetWindowTitles(wind,NULL,"Rummy    <QUIT>: Use Verify Menu To Confirm");
  303.                             }
  304.                         }
  305.                     case 1:
  306.                     case 2:
  307.                         pass=1;
  308.                         break;
  309.                     case 3:
  310.                         /*quit=1;*/
  311.                         quitverf = !quitverf;
  312.                         if(!quitverf)
  313.                         {
  314.                             OnMenu(wind,0xf823);
  315.                             SetWindowTitles(wind,NULL,"Rummy    <QUIT>: Use Verify Menu To Confirm");
  316.                         }
  317.                         else
  318.                         { /*reselect to cancel*/
  319.                             OffMenu(wind,0xf823);
  320.                             if(exgmverf) SetWindowTitles(wind,NULL,"Rummy by Simon Preene, Copyright Amiga Computing 1990");
  321.                             else SetWindowTitles(wind,NULL,"Rummy    <END GAME>: Use Verify Menu To Confirm");
  322.                         }
  323.                         pass=1;    /*must still awaken main program*/
  324.                         break;
  325.                     }
  326.                     break;
  327.                 case INTELLMENU:
  328.                     plr[inum+1]->level = snum+1;
  329.                     break;
  330.                 case DEBUGMENU:
  331.                     switch(inum)
  332.                     {
  333.                     case 0:/*view cards*/
  334.                         plr[snum+1]->viewcard = item->Flags & CHECKED;
  335.                         break;
  336.                     case 1:/*manual deal*/
  337.                         mandeal=item->Flags & CHECKED;
  338.                         break;
  339.                     case 2:/*manual control*/
  340.                         plr[snum+1]->manctrl = item->Flags & CHECKED;
  341.                         break;
  342.                     case 3:/*modify memory*/
  343.                         pass=1;
  344.                         break;
  345.                     case 4:/*initialise pack*/
  346.                         initpack[snum] = item->Flags & CHECKED;
  347.                         break;
  348.                     case 5:/*shuffle pack*/
  349.                         shufpack[snum] = item->Flags & CHECKED;
  350.                         break;
  351.                     case 6:/*continuous play*/
  352.                         contplay = item->Flags&CHECKED;
  353.                         break;
  354.                     }
  355.                     break;
  356.                 case VERIFYMENU:
  357.                     if(inum==0)
  358.                     {
  359.                         exgm=1;
  360.                         exgmverf = !exgmverf;
  361.                         OffMenu(wind,0xf803);
  362.                         if(quitverf) SetWindowTitles(wind,NULL,"Rummy by Simon Preene, Copyright Amiga Computing 1990");
  363.                         else SetWindowTitles(wind,NULL,"Rummy    <QUIT>: Use Verify Menu To Confirm");
  364.                     }
  365.                     else quit=inum==1;
  366.                     pass = quit || exgm;
  367.                 }
  368.                 code=item->NextSelect;
  369.             }
  370.         }
  371.         if(pass) PutMsg(mp,(struct Message *)m1);     /*pass message to main program*/
  372.         else ReplyMsg((struct Message *)m1);
  373.     }
  374.     return signals;
  375. }
  376.  
  377. VOID    prgenmsg(ULONG class,USHORT code,APTR iaddr,struct playerinfo *cp)
  378. {    /* in this function switches are used in places where they are not
  379.         needed to simplify future changes*/
  380.     ULONG    mnum,inum,snum;
  381.     switch(class)
  382.     {
  383.     case GADGETUP:
  384.         break; /*gadget processing handled in main program*/
  385.     case MENUPICK:
  386.         while(code!=MENUNULL)
  387.         {
  388.             mnum=MENUNUM(code);inum=ITEMNUM(code);snum=SUBNUM(code);
  389.             switch(mnum)
  390.             {
  391.             case PROJMENU:
  392.                 switch(inum)
  393.                 {
  394.                 case 1: /*reorder*/
  395.                     reorder(cp);
  396.                     break;
  397.                 case 2:/*about*/
  398.                     about();
  399.                     break;
  400.                 }
  401.                 break;
  402.             case DEBUGMENU:
  403.                 switch(inum)
  404.                 {
  405.                 case 3: /*modify memory*/
  406.                     modmem(plr[snum+1]);
  407.                     break;
  408.                 }
  409.             }
  410.             code=((struct MenuItem *)ItemAddress(menu,(LONG)code))->NextSelect;
  411.         }
  412.     }
  413. }
  414.  
  415. VOID    about()
  416. {
  417.     struct    IntuiMessage    *msg;
  418.     USHORT    fin=0;
  419.     Request(&aboutr,wind);
  420.     while(!fin)
  421.     {
  422.         Wait(1<<mp->mp_SigBit);
  423.         while(msg=(struct IntuiMessage *)GetMsg(mp))
  424.         {
  425.             if(msg->Class == GADGETUP && ((struct Gadget *)(msg->IAddress))->GadgetID==PROCG) fin=1;
  426.             ReplyMsg((struct Message *)msg);
  427.         }
  428.     }
  429. }
  430.  
  431.